<?php

namespace controller;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ShopAdmin_Order
 *
 * @author Damian
 */
class ShopAdmin_Order extends Seller {

    public function index() {
        if (\library\RequestMethods::post("OrderID") && \library\RequestMethods::post("orderStatus")) {
            $order = \model\TblOrder::getModel()->getByPrimaryKey(\library\RequestMethods::post("OrderID"));
            if ($order) {
                $order->Status = \library\RequestMethods::post("orderStatus");
                $order->save();
            }
        }
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Lista zamówień");
        $page = (\library\RequestMethods::get("Page")) ? \library\RequestMethods::get("Page") : 1;
        $orderCriteria = new \model\Criteria();
        $orderCriteria->from(\model\TblOrder::getModel()->getTable())
                ->join(\model\TblDeliveryMethod::getModel())
                ->join(\model\TblPayment::getModel())
                ->join(\model\TblAdress::getModel())
                ->join(\model\TblPerson::getModel())
                ->where(\model\TblOrder::SHOP_ID, "=", $this->shop->ShopID, \model\TblOrder::getModel()->getTable())
                ->order(\model\TblOrder::ORDER_DATE, " desc ")
                ->join(\model\TblCustomer::getModel());
        if (\library\RequestMethods::get("id")) {
            $orderCriteria->where(\model\TblOrder::ID, "=", \library\RequestMethods::get("id"), \model\TblOrder::getModel()->getTable(), " and ");
        }

        $view = $this->getActionView();
        $session = \library\Registry::get("session");
        if (\library\RequestMethods::get("order")) {
            $session->set("orderOrders", \library\RequestMethods::get("order"));
        }
        $view->set("order", $session->get("orderOrders"));
        $order = \model\TblOrder::createOrderString($session->get("orderOrders"));
        $orderCriteria->order($order[0], $order[1], $order[2]);
        $all = $orderCriteria->count();
        $pagesCount = ceil($all / $this->perPage);
        $pages = new \view\Pagination($page, 1, $pagesCount, "./" . $this->shop->URL . "/" . "ShopAdmin_Order");
        $layoutView->set("pages", $pages);
        $orders = \model\TblOrder::getModel()->all($orderCriteria->limit($this->perPage, $page));
        $ordersDataTable = new \model\DataTable($orders);



        $ordersTable = $ordersDataTable->getModelData(true);
        foreach ($ordersTable as &$order) {
            $productsCriteria = new \model\Criteria();
            $productsCriteria->from(\model\TblProduct::getModel()->getTable())
                    ->join(\model\TblProductOrder::getModel())
                    ->where(\model\TblProductOrder::ORDER_ID, "=", $order->OrderID);
            $products = \model\TblProductOrder::getModel()->all($productsCriteria);
            $productsTable = new \model\DataTable($products);
            $order->Products = $productsTable->getModelData(true);
        }
        $view->set("orders", $ordersTable);

        $this->render();
        // var_dump($ordersTable);
    }

}
